/**
 * 
 */
package com.neusoft.cmsprojects.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javassist.bytecode.ByteArray;

import javax.annotation.Resource;
import javax.enterprise.inject.New;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.Blob;
import com.neusoft.cmsprojects.dao.BasicSettingsDao;
import com.neusoft.cmsprojects.model.TbConfig;
import com.neusoft.cmsprojects.util.FileUtil;
import com.neusoft.cmsprojects.util.HtmlUtil;
import com.neusoft.cmsprojects.util.ResponseUtil;
import com.opensymphony.oscache.web.ServletCache;

/**
 * index: 基本设置相关的业务层接口实现
 * 
 * @author guanhaimin date:2014年12月16日下午3:18:35
 */
@Service("BasicSettingsService")
@Transactional
public class BasicSettingsServiceImpl implements BasicSettingsService {

	@Resource(name = "BasicSettingsDao")
	private BasicSettingsDao basicSettingsDao;

	public BasicSettingsDao getBasicSettingsDao() {
		return basicSettingsDao;
	}

	public void setBasicSettingsDao(BasicSettingsDao basicSettingsDao) {
		this.basicSettingsDao = basicSettingsDao;
	}

	@Override
	public void saveBasicSettings(TbConfig tc) {
		this.basicSettingsDao.save(tc);
	}

	@Override
	public void getBasicSettings() {
		HttpServletRequest request = ServletActionContext.getRequest();
		TbConfig tc = this.basicSettingsDao.getBasicSettings();
		if (tc != null) {
			request.setAttribute("TbConfigData", tc);
		}
	}

	@Override
	public void updateBasicSettings(TbConfig tc) throws Exception {
		// TODO Auto-generated method stub
		HttpServletResponse response = ServletActionContext.getResponse();
		String msg = "";
		TbConfig temptc = this.basicSettingsDao.getBasicSettings();
		if (tc != null) {
			if (temptc != null) {
				temptc.setCTitle(tc.getCTitle());
				temptc.setCTitilee(tc.getCTitilee());
				temptc.setCDescription(tc.getCDescription());
				temptc.setCDescriptione(tc.getCDescriptione());
				temptc.setCSearch(tc.getCSearch());
				temptc.setCSearche(tc.getCSearche());
				temptc.setCContact(tc.getCContact());
				temptc.setCContacte(tc.getCContacte());
				temptc.setCMapcode(tc.getCMapcode());
				temptc.setCStatcode(tc.getCStatcode());
				this.basicSettingsDao.update(temptc);
			} else {
				this.basicSettingsDao.save(tc);
			}
			msg = "SaveSuccess";
		} else {
			msg = "TbConfigIsNull";
		}
		ResponseUtil.write(response, msg);
	}

	@Override
	public void getCompanyIntroduceContent() {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		TbConfig tc = this.getBasicSettingsDao().getBasicSettings();
		if (tc != null) {
			if (tc.getCIntro() != null) {
				String cintro = new String(tc.getCIntro());
				request.setAttribute("ZhData", cintro);
			}
			if (tc.getCIntroe() != null) {
				String cintroe = new String(tc.getCIntroe());
				request.setAttribute("EnData", cintroe);
			}
		}
	}

	@Override
	public String saveContent(String content, String opt) throws Exception {
		// TODO Auto-generated method stub
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String msg = "";
		TbConfig tc = this.basicSettingsDao.getBasicSettings();
		int flag = 1;
		if(tc == null){
			tc = new TbConfig();
			flag = 0;
		}
		if (content != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String strdate = sdf.format(new Date());
			FileUtil.CreateDiretory("tempfiles\\" + strdate + "\\");
			content = content.replaceAll("src=\"tempfiles/" + strdate + "/",
					"src=\"/CMSProject/Resources/image/"); // 改变<img/>标签的图片路径
			content = content.replaceAll("href=\"tempfiles/" + strdate + "/",
					"href=\"/CMSProject/Resources/file/"); // 改变<img/>标签的图片路径
			if (opt.trim().equals("CINTRO")) { // 保存中文内容
				System.out.println(tc);
				tc.setCIntro(content.getBytes());
				request.setAttribute("ZhData", content);
				msg = "保存公司简介中文内容成功";
			} else {
				if (opt.trim().equals("CINTROE")) { // 保存英文内容
					if (content.getBytes().length == content.length()) { // 判断是否存在汉字
						byte[] bytes = content.getBytes();
						tc.setCIntroe(bytes);
						request.setAttribute("EnData", content);
						msg = "保存公司简介英文内容成功";
					} else {
						msg = "存在汉字";
					}
				}else{
					if(opt.trim().equals("CCONTACT")){
						tc.setCContact(content.getBytes());
						request.setAttribute("ZhData", content);
						msg = "保存联系我们中文内容成功";
					}else{
						if(opt.trim().equals("CCONTACTE")){
							if(content.getBytes().length == content.length()){
								tc.setCContacte(content.getBytes());
								request.setAttribute("EnData", content);
								msg = "保存联系我们英文内容成功";
							}else{
								msg = "存在汉字";
							}
						}else{
							if(opt.trim().equals("CBOTTOM")){
								tc.setCBottom(content.getBytes());
								request.setAttribute("ZhData", content);
								msg = "保存网站底部中文内容成功";
							}else{
								if(opt.trim().equals("CBOTTOME")){
									if(content.getBytes().length == content.length()){
										tc.setCBottome(content.getBytes());
										request.setAttribute("EnData", content);
										msg = "保存网站底部英文内容成功";
									}else{
										msg = "存在中文";
									}
								}else{
									//TOADD: 此处添加新的功能分支
								}
							}
						}
					}
				}
			}
			if (flag == 1) {
				System.out.println("更新网站基本设置");
				this.basicSettingsDao.update(tc);
			} else {
				System.out.println("保存网站基本设置");
				this.basicSettingsDao.save(tc);
			}
		} else {
			msg = "DataIsNull";
		}
		ResponseUtil.write(response, msg);
		return content;
	}

	@Override
	public String saveFile(String content, String opt) throws Exception {
		// TODO Auto-generated method stub
		TbConfig tc = this.basicSettingsDao.getBasicSettings();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String strdate = sdf.format(new Date());
		String msg = "";
		if (content != null) {
			content = content.replaceAll("src=\"tempfiles/" + strdate + "/",
					"src=\"/CMSProject/Resources/image/"); // 改变<img/>标签的图片路径
			content = content.replaceAll("href=\"tempfiles/" + strdate + "/",
					"href=\"/CMSProject/Resources/file/"); // 改变<a/>标签的附件路径
			if (tc != null) {
				if (opt.trim().equals("CINTRO")) {
					if(tc.getCIntro() != null){
						FileUtil.saveImageFromText("Resources\\image\\",
								"tempfiles\\" + strdate + "\\",
								new String(tc.getCIntro()),
								"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
						FileUtil.saveImageFromText("Resources\\file\\",
								"tempfiles\\" + strdate + "\\",
								new String(tc.getCIntro()),
								"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
						FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
								"Resources\\image\\", content,
								"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中原图片从临时文件夹中取回
						FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
								"Resources\\file\\", content,
								"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中原图片从临时文件夹中取回
					}else{
						FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
								"Resources\\image\\", content,
								"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中图片从临时文件夹中取回
						FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
								"Resources\\file\\", content,
								"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
					}
					FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
							+ "\\");
				} else {
					if (opt.trim().equals("CINTROE")) {
						if (tc.getCIntroe() != null) {
							FileUtil.saveImageFromText("Resources\\image\\",
									"tempfiles\\" + strdate + "/",
									new String(tc.getCIntroe()),
									"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
							FileUtil.saveImageFromText("Resources\\file\\",
									"tempfiles\\" + strdate + "\\",
									new String(tc.getCIntroe()),
									"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
							FileUtil.saveImageFromText("tempfiles\\" + strdate
									+ "/", "Resources\\image\\", content,
									"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
							FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
									"Resources\\file\\", content,
									"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中原附件从临时文件夹中取回
						}else{
							FileUtil.saveImageFromText("tempfiles\\" + strdate
									+ "/", "Resources\\image\\", content,
									"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
							FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
									"Resources\\file\\", content,
									"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
						}
						FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
								+ "\\");
					}else{
						if(opt.trim().equals("CCONTACT")){
							if(tc.getCContact() != null){
								FileUtil.saveImageFromText("Resources\\image\\",
										"tempfiles\\" + strdate + "/",
										new String(tc.getCContact()),
										"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
								FileUtil.saveImageFromText("Resources\\file\\",
										"tempfiles\\" + strdate + "\\",
										new String(tc.getCContact()),
										"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
								FileUtil.saveImageFromText("tempfiles\\" + strdate
										+ "/", "Resources\\image\\", content,
										"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
								FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
										"Resources\\file\\", content,
										"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
								
							}else{
								FileUtil.saveImageFromText("tempfiles\\" + strdate
										+ "/", "Resources\\image\\", content,
										"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
								FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
										"Resources\\file\\", content,
										"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
							}
							FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
									+ "\\");
						}else{
							if(opt.trim().equals("CCONTACTE")){
								if(tc.getCContacte() != null){
									FileUtil.saveImageFromText("Resources\\image\\",
											"tempfiles\\" + strdate + "/",
											new String(tc.getCContacte()),
											"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
									FileUtil.saveImageFromText("Resources\\file\\",
											"tempfiles\\" + strdate + "/",
											new String(tc.getCContacte()),
											"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
									FileUtil.saveImageFromText("tempfiles\\" + strdate
											+ "/", "Resources\\image\\", content,
											"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
									FileUtil.saveImageFromText("tempfiles\\" + strdate
											+ "/", "Resources\\file\\", content,
											"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中折原附件从临时文件夹中取回
								}else{
									FileUtil.saveImageFromText("tempfiles\\" + strdate
											+ "/", "Resources\\image\\", content,
											"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
									FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
											"Resources\\file\\", content,
											"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
								}
								FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
										+ "\\");
							}else{
								if(opt.trim().equals("CBOTTOM")){
									if(tc.getCBottom() != null){
										FileUtil.saveImageFromText("Resources\\image\\",
												"tempfiles\\" + strdate + "/",
												new String(tc.getCBottom()),
												"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
										FileUtil.saveImageFromText("Resources\\file\\",
												"tempfiles\\" + strdate + "/",
												new String(tc.getCBottom()),
												"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
										FileUtil.saveImageFromText("tempfiles\\" + strdate
												+ "/", "Resources\\image\\", content,
												"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
										FileUtil.saveImageFromText("tempfiles\\" + strdate
												+ "/", "Resources\\file\\", content,
												"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中折原附件从临时文件夹中取回
									}else{
										FileUtil.saveImageFromText("tempfiles\\" + strdate
												+ "/", "Resources\\image\\", content,
												"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
										FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
												"Resources\\file\\", content,
												"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
									}
									FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
											+ "\\");
								}else{
									if(opt.trim().equals("CBOTTOME")){
										if(tc.getCBottome() != null){
											FileUtil.saveImageFromText("Resources\\image\\",
													"tempfiles\\" + strdate + "/",
													new String(tc.getCBottome()),
													"src=\"/CMSProject/Resources/image/(.*?)\"");// 将文章中的原图片转存到临时文件夹
											FileUtil.saveImageFromText("Resources\\file\\",
													"tempfiles\\" + strdate + "/",
													new String(tc.getCBottome()),
													"href=\"/CMSProject/Resources/file/(.*?)\"");// 将文章中的原附件转存到临时文件夹
											FileUtil.saveImageFromText("tempfiles\\" + strdate
													+ "/", "Resources\\image\\", content,
													"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
											FileUtil.saveImageFromText("tempfiles\\" + strdate
													+ "/", "Resources\\file\\", content,
													"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中折原附件从临时文件夹中取回
										}else{
											FileUtil.saveImageFromText("tempfiles\\" + strdate
													+ "/", "Resources\\image\\", content,
													"src=\"/CMSProject/Resources/image/(.*?)\""); // 将文章中折原图片从临时文件夹中取回
											FileUtil.saveImageFromText("tempfiles\\" + strdate + "\\",
													"Resources\\file\\", content,
													"href=\"/CMSProject/Resources/file/(.*?)\""); // 将文章中附件从临时文件夹中取回
										}
										FileUtil.deleteTheFolderOfFiles("tempfiles\\" + strdate
												+ "\\");
									}else{
										//TOADD:	在此添加新的功能分支
									}
								}
							}
						}
					}
				}
			} else {
				System.out.println("图片转存");
				content = content.replaceAll(
						"src=\"tempfiles/" + strdate + "/",
						"src=\"/CMSProject/Resources/image/"); // 改变<img/>标签的图片路径
				content = content.replaceAll(
						"src=\"tempfiles/" + strdate + "/",
						"src=\"/CMSProject/Resources/file/"); // 改变<img/>标签的图片路径
				FileUtil.saveImageFromText("tempfiles\\" + strdate + "/",
						"\\Resources\\image\\", content,
						"src=\"/CMSProject/Resources/image/(.*?)\""); // 将临时文件夹里面的图片移到指定的资源文件夹中
				FileUtil.saveImageFromText("tempfiles\\" + strdate + "/",
						"\\Resources\\file\\", content,
						"href=\"/CMSProject/Resources/file/(.*?)\""); // 将临时文件夹里面的图片移到指定的资源文件夹中
			}
			msg = "SaveSuccess";
		}else{
			msg = "DataIsNull";
		}
		return msg;
	}

	@Override
	public void getCompanyContactContent() {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		TbConfig tc = this.getBasicSettingsDao().getBasicSettings();
		if (tc != null) {
			if (tc.getCContact() != null) {
				String ccontact = new String(tc.getCContact());
				request.setAttribute("ZhData", ccontact);
			}
			if (tc.getCContacte() != null) {
				String ccontacte = new String(tc.getCContacte());
				request.setAttribute("EnData", ccontacte);
			}
		}
	}

	@Override
	public void getSiteBottomContent() {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		TbConfig tc = this.basicSettingsDao.getBasicSettings();
		if(tc != null){
			if(tc.getCBottom() != null){
				String cbottom = new String(tc.getCBottom());
				request.setAttribute("ZhData", cbottom);
			}
			if(tc.getCBottome() != null){
				String cbottome = new String(tc.getCBottome());
				request.setAttribute("EnData", cbottome);
			}
		}
	}

}
